/*
  This file is part of Open-Doors.

  Open-Doors is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  Open-Doors is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with Open-Doors.  If not, see <http://www.gnu.org/licenses/>.

  Authors: Rune Thorbek & Araz B. Makoo
*/ 

var venue_win;

function renderTip(val, meta, rec, rowIndex, colIndex, store) {
    meta.tdAttr = 'data-qtip="' + val.replace(new RegExp('\ ', 'g'), "&nbsp") + '"';
    return val;
}

function showVenueForm(title, edit, data) {
    if (!venue_win) {
        var form = Ext.widget('form', {
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            border: false,
            bodyPadding: 10,
	    
            fieldDefaults: {
                labelAlign: 'top',
                labelWidth: 100,
                labelStyle: 'font-weight:bold',
		labelSeparator: "" 
            },
                defaults: {
                    margins: '0 0 0 0'
                },
	    
            
	    items: [{
		xtype: 'textfield',
		id: 'form_venue_edit',
                fieldLabel: 'edit',
		hidden: true
	    }, {
                xtype: 'textfield',
		id: 'form_venue_uid',
                fieldLabel: 'uid',
		hidden: true
	    }, {
                xtype: 'textfield',
		id: 'form_venue_code',
                fieldLabel: 'Kode',
	    }, {
                xtype: 'textfield',
		id: 'form_venue_name',
                fieldLabel: 'Navn*',
		allowBlank: false,
	    }, {
                xtype: 'textareafield',
		id: 'form_venue_address',
                fieldLabel: 'Adresse',
                labelAlign: 'top',
                flex: 1,
                margins: '0',
	    }],
	    
	    buttons: [{
                text: 'Ok',
                handler: function() {
                    if (this.up('form').getForm().isValid()) {
			commit_venue();
                        this.up('form').getForm().reset();
                        this.up('window').hide();
		    }
                }
            }, {
                text: 'Annuller',
                handler: function() {
                    this.up('form').getForm().reset();
                    this.up('window').hide();
                }
            }]
        });
	
        venue_win = Ext.widget('window', {
            title: title,
            closeAction: 'hide',
            width: 400,
            height: 280,
            layout: 'fit',
            resizable: false,
            modal: true,
            items: form
        });
    }

    venue_win.down('form').getForm().reset();
    if (edit) {
	Ext.getCmp('form_venue_edit').setValue(edit);
	Ext.getCmp('form_venue_uid').setValue(data.get('uid'));
	Ext.getCmp('form_venue_code').setValue(data.get('code'));
	Ext.getCmp('form_venue_name').setValue(data.get('name'));
	Ext.getCmp('form_venue_address').setValue(data.get('address'));
    }

    venue_win.setTitle(title);
    venue_win.show();
}


function commit_venue() {
  
    var edit_value = Ext.getCmp('form_venue_edit').getValue();
    var uid_value = Ext.getCmp('form_venue_uid').getValue();
    var code_value = Ext.getCmp('form_venue_code').getValue();
    var name_value = Ext.getCmp('form_venue_name').getValue();
    var address_value = Ext.getCmp('form_venue_address').getValue();
   
    Ext.Ajax.request({
	url: 'db_functions.php',
	method: 'post',
	params: {
	    function  : 'commit_venue',
	    edit: edit_value,
	    uid: uid_value,
	    code: code_value,
	    name: name_value,
	    address: address_value
	},
	success: function(response, object) {
	    var res = Ext.decode(response.responseText);
	 
            if (res.success == false){
		Ext.MessageBox.alert("Fejl", res.msg);
	    } else {
		var selection = Ext.getCmp("venues_grid").getSelectionModel().getSelection();
		
		if (selection.length != 0) {
		    selection[0].data = res.results[0]
		    var index = Ext.getCmp("venues_grid").getStore().indexOf(selection[0]);
		    Ext.getCmp("venues_grid").getView().refreshNode(index);
		}

		//Ext.MessageBox.alert("Info", res.msg);
	    }
	    if (edit_value == null || edit_value == '') {
		search("venues");
	    }
	    search("kampagner");
	    search("indsamlere");
	    
	    
	}
    });
}

function edit_venue(data) {
    showVenueForm('Rediger mødested', true, data);
}

var contextmenu_venues = new Ext.menu.Menu({
    items: [
	{ 
	    text: 'Rediger',
	    icon: 'app/view/edit_row.gif',
	    id: 'edit_venue_menuitem',
	    hidden: true,
	    handler: function() {
		var selection = Ext.getCmp("venues_grid").getSelectionModel().getSelection();
		if (selection.length > 0) {
		    edit_venue(selection[0]);
		}
	    }
	}, { 
	    text: 'Slet',
	    icon: 'app/view/delete_row.gif',
	    id: 'delete_venue_menuitem',
	    hidden: true,
	    handler: function() {
		var selection = Ext.getCmp("venues_grid").getSelectionModel().getSelection();
		if (selection.length > 0) {
		    Ext.Msg.confirm('Spørgsmål', 'Er du sikker på, at du vil slette rækken?', function(btn, text){
			if (btn == 'yes'){
			    delete_row("venues", "delete_venue", selection[0].get('uid'));
			    return;
			}
		    });
		}
	    }
	}
    ]
});

Ext.define('OpenDoors.view.VenuesView', {
    extend: 'Ext.panel.Panel',
    alias: 'widget.venueview',

    hideHeaders: true,
    

    items: [{
	xtype: 'component',
	html: '<font size="5">Mødesteder</font>',
 	margin: "30 0 0 30"
	
    }, {
	xtype: 'container',
	width: 800,
	layout: 'hbox',
	border: false,
	items: [{
	    xtype: 'textfield',
	    id: 'search_venues_field',
	    width: 300,
	    margin: "50 0 0 30",
	    listeners:{  
		scope:this,  
		specialkey: function(f,e) {  
		    if(e.getKey()==e.ENTER) {  
			search("venues");
		    }  
		}  
	    }
	}, {
	    xtype: 'button',
	    width: 100,
	    text: 'Søg/Opdater',
	    id: 'search_venues_button',
	    margin: "50 0 0 10"
	}, {
	    xtype: 'button',
	    width: 100,
	    text: 'Tilføj...',
	    hidden: true,
	    id: 'add_venue_button',
	    margin: "50 0 0 10"
	}, {
	    xtype: 'label',
	    text: 'Total indsamlet beløb: 0',
	    id: 'total_indsamlet_label',
	    margin: "50 0 0 50"
	}]
    }, {
	xtype: 'grid',
	id: 'venues_grid',
	columnLines: true,
	margin: "30 15 30 15",
	store: 'Venues',
	verticalScroller: {
	    xtype: 'paginggridscroller'
	},
	listeners: {
	    beforeitemcontextmenu: function(view, record, item, index, e)
	    {
		e.stopEvent();
		contextmenu_venues.showAt(e.getXY());
	    }
	},
	
	columns: [{
	    xtype: 'rownumberer',
	    sortable: false,
	    width: 50
	}, {
	    text: 'Kode',
	    dataIndex: 'code',
	    align:'right', 
	    style: 'text-align:left',
	    width: 70,
	    renderer: renderTip
	}, {
	    text: 'Navn',
	    dataIndex: 'name',
	    flex: 1,
	    renderer: renderTip
	}, {
	    text: 'Total beløb',
	    dataIndex: 'total',
	    align:'right', 
	    style: 'text-align:left',
	    flex: 1,
	    renderer: renderTip
	}, {
	    text: 'Gennemsnit pr. gruppe',
	    dataIndex: 'average_collected',
	    align:'right', 
	    style: 'text-align:left',
	    flex: 1,
	    renderer: renderTip
	}, {
	    text: 'Antal indsamlere',
	    dataIndex: 'no_collectors',
	    align:'right', 
	    style: 'text-align:left',
	    flex: 1,
	    renderer: renderTip
	}, {
	    text: 'Antal ruter',
	    dataIndex: 'no_routes',
	    align:'right', 
	    style: 'text-align:left',
	    flex: 1,
	    renderer: renderTip
	}, {
	    text: 'Antal grupper',
	    dataIndex: 'no_groups',
	    align:'right', 
	    style: 'text-align:left',
	    flex: 1,
	    renderer: renderTip
	}, {
	    text: 'Antal besatte ruter',
	    dataIndex: 'taken_routes',
	    align:'right', 
	    style: 'text-align:left',
	    flex: 1,
	    renderer: renderTip
	}]
    }],
	   
	
    initComponent: function() {
        
        this.callParent();
    }
});

